using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace Skeletons
{
    enum eDBItemType
    {
        ITM_STRING,
        ITM_INTEGER,
        ITM_DOUBLE
    }

    public class DB
    {
        private MySqlConnection m_rConn;
        private MySqlCommand m_rCommand;
        private MySqlDataReader m_rReader = null;
        private long m_iLastAutoincrementedID = -1;

        public DB(String p_strConnectionString)
        {
            if (p_strConnectionString.Length == 0)
                return;

            m_rConn = new MySql.Data.MySqlClient.MySqlConnection(p_strConnectionString);

            bool bErr = false;

            try
            {
                m_rConn.Open();
            }
            catch
            {
               Console.WriteLine("DB Connection Refused!! ");
               bErr = true;
            }

            if (!bErr)
            {
                // Initialize if connection's success!

                m_rCommand = new MySqlCommand();
                m_rCommand.Connection = m_rConn;

                /*
                cmd.CommandText = "insert into artia.user (U_Type, U_Username, U_Password) value ('1', 'VSS', 'ikko');";
                cmd.ExecuteNonQuery();

                cmd.CommandText = "SELECT * FROM user";
                MySqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    System.Console.Out.WriteLine(rdr["U_Username"].ToString()); // legge il primo campo, di tipo string
                }
            

                rdr.Close();
                */
            }
        }

        public bool isConnected()
        {
            if (m_rConn.State == System.Data.ConnectionState.Open)
                return true;
            return false;
        }

        public MySqlDataReader query(String p_strQuery)
        {
            MySqlDataReader rReader = null;

            m_rCommand.CommandText = p_strQuery;
            
            try
            {
                rReader = m_rCommand.ExecuteReader();
            }
            catch(MySqlException exc)
            {
                Console.WriteLine("ATTENZIONE: " + exc.ToString());
            }

            return rReader;
        }

        public bool save(string p_strQuery)
        {
            m_rCommand.CommandText = p_strQuery;

            int res = 0;
           
            try
            {
                res = m_rCommand.ExecuteNonQuery();

                m_iLastAutoincrementedID = m_rCommand.LastInsertedId;

                return true;
            }
            catch(MySqlException ex)
            {
                Console.WriteLine("Problemi nell'esecuzione della query: " + ex.ToString());
                m_iLastAutoincrementedID = -1;
                return false;
            }
        }

        public long getLastAutoincrementedID()
        {
            return m_iLastAutoincrementedID;
        }

        public bool recordExists(string p_strTable, string p_strIDField, long p_iID)
        {
            return recordExists(p_strTable, p_strIDField, p_iID.ToString());
        }

        public bool recordExists(string p_strTable, string p_strIDField, string p_strValue)
        {
            bool res = true;
            string strQuery = "SELECT COUNT(*) FROM " + p_strTable + " WHERE " + p_strIDField + " = '" + p_strValue + "'";

            MySqlDataReader rReader = query(strQuery);
            if (rReader != null)
            {
                rReader.Read();

                Console.WriteLine("m_rReader[0]: " + rReader[0]);

                if (int.Parse(rReader[0].ToString()) > 0)
                    res = true;
                else
                    res = false;

                rReader.Close();
            }

            return res;
        }

    }
}
